package com.ipzoe.act.service.impl;

import cn.hutool.core.date.DateUtil;
import com.ipzoe.act.domain.Activity;
import com.ipzoe.act.domain.ActivitySignup;
import com.ipzoe.act.mapper.ActivitySignupMapper;
import com.ipzoe.act.service.IActivityService;
import com.ipzoe.act.service.IActivitySignupService;
import com.ipzoe.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * 活动报名Service业务层处理
 *
 * @author eric
 * @date 2020-08-20
 */
@Service
public class ActivitySignupServiceImpl extends ServiceImpl<ActivitySignupMapper, ActivitySignup> implements IActivitySignupService {

    @Autowired
    private IActivityService iActivityService;

    @Override
    public void signin(Long id, String username) {

        Date today = new Date();
        ActivitySignup activitySignup = getById(id);
        if(activitySignup == null){
            throw new CustomException(400,"未找到相关活动报名记录");
        }

        Activity activity = iActivityService.getById(activitySignup.getActivityId());
        if(activity==null){
            throw new CustomException(400,"未找到相关活动");
        }

        Date beginOfDayBeginTime = DateUtil.beginOfDay(activity.getBeginTime());
        Date beginOfDayEndTime = DateUtil.beginOfDay(activity.getEndTime());
        Date beginOfDayToday = DateUtil.beginOfDay(today);
        if(activity.getStatus()==0 || beginOfDayToday.before(beginOfDayBeginTime)){
            throw new CustomException(400,"活动未开始，不可签到");
        }
        if(activity.getStatus()==2 || beginOfDayToday.after(beginOfDayEndTime)){
            throw new CustomException(400,"活动已结束，不可签到");
        }

        activitySignup.setStatus(1);
        activitySignup.setVerificationBy(username);
        activitySignup.setVerificationTime(new Date());

        updateById(activitySignup);
    }
}
